<?php
class Zend_Controller_Action_Helper_Alipay extends Zend_Controller_Action_Helper_Abstract{
	
	private $return_url;
	
	private $notify_url;
	
	public function setReturnUrl($return_url){
		
		$this->return_url=$return_url;
		
	}
	
	public function setNotifyUrl($notify_url){
		
		$this->notify_url=$notify_url;
		
	}
	
	public function createLink($subject,$orderid,$total){

		require_once(MY_ROOT."/models/alipay/alipay_service.php");
		
    	require_once(MY_ROOT.'/php_config/alipay_config.php');

		
		$parameter = array(
			"service"         => "create_direct_pay_by_user",  //交易类型
			"partner"         => $partner,          //合作商户号
			"return_url"      => $this->return_url,       //同步返回
			"notify_url"      => $this->notify_url,        //异步返回
			"_input_charset"  => $_input_charset,   //字符集，默认为GBK
			"subject"         => $subject,        //商品名称，必填
			"body"            => subject,        //商品描述，必填
			"out_trade_no"    => $orderid,      //商品外部交易号，必填（保证唯一性）
			"total_fee"       => $total,            //商品单价，必填（价格不能为0）
			"payment_type"    => "1",               //默认为1,不需要修改
		
			"show_url"        => $show_url,         //商品相关网站
			"seller_email"    => $seller_email      //卖家邮箱，必填
		);
		$alipay = new alipay_service($parameter,$security_code,$sign_type);
		
		$link=$alipay->create_url();
		
		echo "<script>window.location =\"$link\";</script>";
		
		return $link;
    	
	}
	
	public function notifyExpressOrder($orderid){
		
		require_once(MY_ROOT."/models/alipay/alipay_notify.php");
		
		require_once(MY_ROOT.'/php_config/alipay_config.php');
		
		$order=new COrder();
		
		$orderArray=$order->getDetail($orderid);
		
		$alipay = new alipay_notify($partner,$security_code,$sign_type,$_input_charset,$transport);
		
		$verify_result = $alipay->notify_verify();
		
		if($verify_result) {

			$dingdan=$_POST['out_trade_no'];    //获取支付宝传递过来的订单号
			$total=$_POST['total_fee'];    //获取支付宝传递过来的总价格
			
		    $receive_name    =$_POST['receive_name'];   //获取收货人姓名
			$receive_address =$_POST['receive_address']; //获取收货人地址
			$receive_zip     =$_POST['receive_zip'];  //获取收货人邮编
			$receive_phone   =$_POST['receive_phone']; //获取收货人电话
			$receive_mobile  =$_POST['receive_mobile']; //获取收货人手机	
			
			$trade_status=$_POST['trade_status'];    //获取支付宝反馈过来的状态,根据不同的状态来更新数据库 WAIT_BUYER_PAY(表示等待买家付款);WAIT_SELLER_SEND_GOODS(表示买家付款成功,等待卖家发货);WAIT_BUYER_CONFIRM_GOODS(卖家已经发货等待买家确认);TRADE_FINISHED(表示交易已经成功结束)

			if($_POST['trade_status'] == 'TRADE_FINISHED') {

				//这里放入你自定义代码,比如根据不同的trade_status进行不同操作
				
				$orderArray[0]['shipping_cost']=$this->view->numberFormat($orderArray[0]['shipping_cost']-$orderArray[0]['coupon'],2,false);
					
				$total=$this->view->numberFormat($total,2,false);
				
				if($orderArray[0]['shipping_cost']==$total){
				
					$order->editOrder($orderid,array('has_paid'=>1));
					
					$orderArray=$order->getDetail($orderid);
						
					if($orderArray[0]['uid']!='0'){
			
						$mycoupon=new CCoupon();
			
						$mycoupon->subCoupon($orderArray[0]['coupon'],"WHERE uid='".$orderArray[0]['uid']."'");
					
					}
				
				}else{
					
					echo "fail";
					
				}
				
			}
			
		}
		else  {
			
			echo "fail";
			
		}
		
		$order->editOrder($orderid,array('payment'=>'alipay'));
		
	}
	
	public function notifyVirtualMoney($accountid){
		
		require_once(MY_ROOT."/models/alipay/alipay_notify.php");
		
		require_once(MY_ROOT.'/php_config/alipay_config.php');
		
		$account=new CAccount();
		
		$accountArray=$account->getDetail($accountid);
		
		$alipay = new alipay_notify($partner,$security_code,$sign_type,$_input_charset,$transport);
		
		$verify_result = $alipay->notify_verify();
		
		if($verify_result) {

			$dingdan=$_POST['out_trade_no'];    //获取支付宝传递过来的订单号
			$total=$_POST['total_fee'];    //获取支付宝传递过来的总价格
			
		    $receive_name    =$_POST['receive_name'];   //获取收货人姓名
			$receive_address =$_POST['receive_address']; //获取收货人地址
			$receive_zip     =$_POST['receive_zip'];  //获取收货人邮编
			$receive_phone   =$_POST['receive_phone']; //获取收货人电话
			$receive_mobile  =$_POST['receive_mobile']; //获取收货人手机	
			
			$trade_status=$_POST['trade_status'];    //获取支付宝反馈过来的状态,根据不同的状态来更新数据库 WAIT_BUYER_PAY(表示等待买家付款);WAIT_SELLER_SEND_GOODS(表示买家付款成功,等待卖家发货);WAIT_BUYER_CONFIRM_GOODS(卖家已经发货等待买家确认);TRADE_FINISHED(表示交易已经成功结束)

			if($_POST['trade_status'] == 'TRADE_FINISHED') {

				//这里放入你自定义代码,比如根据不同的trade_status进行不同操作
				
				if($accountArray[0]['should_paid']==$total){
				
					$account->editAccount($accountid,array('has_paid'=>$total));
				
				}else{
					
					echo "fail";
					
				}
				
			}
			
		}
		else  {
			
			echo "fail";
			
		}
		
	}
	

	
}
